#include "widget.h"
#include "ui_widget.h"
#include "qpushbutton.h"
#include "newwidget.h"
#include <QtDebug>
/*
 *老师类
 *学生类
 *
 *需求 下课了，老师说饿了，学生请客吃饭！
*/

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //创建老师类和学生类的对象
    this->zt = new teacher(this);//信号
    this->st = new student(this);//信号槽

    /*------信号与信号槽classIsOver()函数触发---*/
    //无参信号和槽连接()
    //指针接收信号地址
    void(teacher::*teacherSignal)() = &teacher::hungry;
    void(student::*studentSignal)() = &student::treat;
    //信号连接
    //connect(zt,&teacher::hungry,st,&student::treat);
    connect(zt,teacherSignal,st,studentSignal);

    //先信号连接后调用下课函数，触发hungry信号
    classIsOver();

    /*----------信号与信号槽使用按钮触发----*/
    //无参信号和槽连接
    QPushButton *btn = new QPushButton("无参下课",this);
    resize(600,400);
    //指针接收信号地址
    void(teacher::*teacherSignal1)() = &teacher::hungry;
    void(student::*studentSignal1)() = &student::treat;
    connect(btn,&QPushButton::clicked,zt,teacherSignal1);//按钮点击触发与hungry槽连接
    connect(zt,teacherSignal1,st,studentSignal1);//hungry信号与treat槽连接

    //有参信号和槽连接
    QPushButton *btn2 = new QPushButton("有参下课",this);
    resize(600,400);
    btn2->move(100,0);
    //指针接收信号地址
    void(teacher::*teacherSignal2)(QString) = &teacher::hungry;
    void(student::*studentSignal2)(QString) = &student::treat;
    connect(btn2,&QPushButton::clicked,this,&Widget::classIsOver);//按钮点击触发与classIsOver槽连接
    connect(zt,teacherSignal2,st,studentSignal2);//hungry信号与treat槽连接

    /*----------断开连接-----*/
    disconnect(btn2,&QPushButton::clicked,this,&Widget::classIsOver);

    /*----扩展------*/
    //信号是可以连接信号的
    //一个信号可以连接多个槽函数
    //多个信号可以连接同一个槽函数
    //信号和槽函数的参数必须类型一一对应
    //信号和槽的参数个数不一定一致，信号的参数个数可以多余槽函数的参数个数

    /*----lambda表达式---*/
    QPushButton *btn3 = new QPushButton();
    btn3->setParent(this);
    btn3->setText("lambda关闭");
    btn3->move(200,0);
    connect(btn3,&QPushButton::clicked,this,[=](){
        this->close();
    });

    /*----lambda练习---*/
    QPushButton *btn4 = new QPushButton("打开新窗口",this);
    btn4->move(200,200);
    newWidget *new_widget = new newWidget();
    connect(btn4,&QPushButton::clicked,this,[=](){
        if(btn4->text()=="打开新窗口"){
            new_widget->show();
            btn4->setText("关闭新窗口");
        }else{
            new_widget->close();
            btn4->setText("打开新窗口");
        }

    });


}
//信号触发函数
void Widget::classIsOver(){
    //触发emit
//    emit zt->hungry();
    emit zt->hungry("钵钵鸡");
}

Widget::~Widget()
{
    delete ui;
}

